package com.simple.json.clazz.basic;

import java.lang.reflect.Field;

import org.json.JSONObject;

import com.simple.json.Util;
import com.simple.json.clazz.ClassInterface;

public class BasicIntClass implements ClassInterface {

	private BasicIntClass() {
	}

	private final static BasicIntClass instance = new BasicIntClass();

	public static BasicIntClass getInstance() {
		return instance;
	}

	@Override
	public Class<?> getProcessClassType() {
		return int.class;
	}

	@Override
	public <T> boolean setRightValueToField(T targetObject, Field targetField,
			JSONObject jsonObject) {
		Class<?> fieldClass = targetField.getType();
		if (!fieldClass.getName().equals(getProcessClassType().getName())) {
			return false;
		}
		
		try {
			int value = jsonObject.optInt(targetField.getName() , 0);
//			targetField.setAccessible(true);
			targetField.setInt(targetObject, value);
			Util.logout("set " + targetField.getName() + "'s int value = " + value);
			return true;
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}

		return false;
	}

	@Override
	public Object getRightValue(Object value) {
		return IntegerClass.getInstance().getRightValue(value);
	}

}
